﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for TopTenManager
/// </summary>
public class TopTenManager
{
    private debioscoopEntities db;

    public TopTenManager(debioscoopEntities db)
	{
        this.db = db;
	}

    public List<film> getTopTenMovies()
    {
        List<film> topTenMovies = new List<film>();

        foreach (var tempMovie in db.top_tien)
        {
            topTenMovies.Add(tempMovie.film);
        }

        return topTenMovies;
    }

    public List<film> getRandomNewsItemMovies()
    {
        List<film> randomMovies = new List<film>();

        film randMovie = null;

        for (int i = 0; i < 3; i++)
        {
            randMovie = getRandomMovie(getTopTenMovies());

            while (randomMovies.Contains(randMovie=getRandomMovie(getTopTenMovies())))
            {
            }

            randomMovies.Add(randMovie);
        }

        return randomMovies;
    }

    private film getRandomMovie( List<film> movies )
    {
        film randMovie = null;

        Random rand = new Random();

        int index = 0;
        int randNumber = rand.Next( movies.Count );

        foreach (var tempMovie in movies)
        {
            if (index == randNumber)
            {
                randMovie = tempMovie;
            }

            index++;
        }

        return randMovie;
    }

    public int getPosition(film movie)
    {
        int pos = 1;

        foreach (var tempMovie in db.top_tien)
        {
            if (tempMovie.film_id == movie.film_id)
            {
                pos = tempMovie.positie;
                break;
            }
        }

        return pos;
    }
}